# 商城子系统
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship, backref
from utils.dataBase import Base


class Store(Base):
    __tablename__ = "store"
    sid = Column(Integer, unique=True, primary_key=True)
    sname = Column(String(50), unique=False)
    spic = Column(String(0), unique=False)
    sprice = Column(Integer, unique=False)
    sinfo = Column(String(255), unique=False)
    snumber = Column(Integer, unique=False)

    def __init__(self, sname, spic, sprice, sinfo, snumber):
        self.sname = sname
        self.spic = spic
        self.sprice = sprice
        self.sinfo = sinfo
        self.snumber = snumber

    def __repr__(self):
        return '<Store %r>' % self.sname


class Collect(Base):
    __tablename__ = "collect"
    cid = Column(Integer, unique=True, primary_key=True)
    uid = Column(Integer, ForeignKey('user.uid'))
    sid = Column(Integer, ForeignKey('store.sid'))

    user = relationship('User', backref=backref('collect', lazy=True))
    store = relationship('Store', backref=backref('collect', lazy=True))

    def __init__(self, uid, sid):
        self.uid = uid
        self.sid = sid

    def __repr__(self):
        return '<Collect %r>' % self.cid


class History(Base):
    __tablename__ = "history"
    hid = Column(Integer, unique=True, primary_key=True)
    uid = Column(Integer, ForeignKey('user.uid'))
    sid = Column(Integer, ForeignKey('store.sid'))
    hnumber = Column(Integer, unique=False)
    hprice = Column(Integer, unique=False)
    time = Column(String(30), unique=False)

    user = relationship('User', backref=backref('History', lazy=True))
    store = relationship('Store', backref=backref('History', lazy=True))

    def __init__(self, uid, sid, hnumber, hprice, time):
        self.uid = uid
        self.sid = sid
        self.hnumber = hnumber
        self.hprice = hprice
        self.time = time

    def __repr__(self):
        return '<History %r>' % self.hid


class Shoppingcar(Base):
    __tablename__ = "shoppingcar"
    scid = Column(Integer, unique=True, primary_key=True)
    uid = Column(Integer, ForeignKey('user.uid'))
    sid = Column(Integer, ForeignKey('store.sid'))

    user = relationship('User', backref=backref('shoppingcar', lazy=True))
    store = relationship('Store', backref=backref('shoppingcar', lazy=True))

    def __init__(self, uid, sid):
        self.uid = uid
        self.sid = sid

    def __repr__(self):
        return '<Shoppingcar %r>' % self.scid
